﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TransitionEffects;
using System.Windows.Media.Animation;
using System.ComponentModel;

namespace CrazyClient
{
    /// <summary>
    /// Interaction logic for FlatDisplay.xaml
    /// </summary>
    /// 
    public class test:DependencyObject
    {
       
    }
    public partial class FlatDisplay : UserControl 
    {
        public FlatDisplay()
        {
            EffectName = "TransitionEffects.RadialBlurTransitionEffect,TransitionEffects";
            InitializeComponent();
            if (LicenseManager.UsageMode != LicenseUsageMode.Designtime) 
            { this.Width = double.NaN; ;      
                this.Height = double.NaN; ;
                this.Margin = new Thickness(0);

            }
        }


        #region LyricIdProperty
        public static readonly DependencyProperty LyricIdProperty =
            DependencyProperty.RegisterAttached("LyricId", typeof(int),
            typeof(UIElement), new PropertyMetadata(OnLyricIdChanged)
            );

        public Visual LastBrush;
        static private void OnLyricIdChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
        
        }
        #endregion

        void sb_Completed(object sender, EventArgs e)
        {
            string st = Storyboard.GetTargetName(((ClockGroup)sender).Timeline);
            TransitionEffect ef = (TransitionEffect)this.FindName(st);
            UIElement control = null;
            foreach (UIElement a in MainCanvas.Children)
            {
                if (a.Effect == ef)
                {
                    control = (UIElement)((VisualBrush)((TransitionEffect)a.Effect).OldImage).Visual;
                    a.Effect = null;
                }
            }
      //      if (control != null)
    //            MainCanvas.Children.Remove(control);

             //   ((Storyboard)sender)
        }




        
        public static void SetLyricId(UIElement obj, int value)
        {
            obj.SetValue(LyricIdProperty, value);
        }



        public static int GetLyricId(DependencyObject obj)
        {
            object result = obj.GetValue(LyricIdProperty);
            return (int)result;
        }
       



        #region EffectNameProperty
        public static readonly DependencyProperty EffectNameProperty =
            DependencyProperty.RegisterAttached("EffectName", typeof(string),
            typeof(FlatDisplay)
            );

        private TransitionEffect GetTransitionEffect()
        {

            return (TransitionEffect)Activator.CreateInstance(Type.GetType(EffectName));
           
            //e.NewValue
        }

        public string EffectName
        {
            set
            {
                SetValue(EffectNameProperty, value);
            }
            get
            {
                object result = GetValue(EffectNameProperty);
                return (string)result;
            }
        }

        #endregion




        #region LyricControlProperty
        public static readonly DependencyProperty LyricControlProperty =
            DependencyProperty.Register("LyricControl", typeof(string),
            typeof(FlatDisplay),
            new PropertyMetadata(OnLyricControlChanged));

        static private void OnLyricControlChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {


            Canvas rectangle1 = (Canvas)((FlatDisplay)obj).MainCanvas.Children[0];


            Canvas newCanvas = new Canvas();
            newCanvas.Height = Double.NaN;
            newCanvas.Width = Double.NaN;
            newCanvas.Background = (Brush)((FlatDisplay)obj).FindResource(e.NewValue);
           // newCanvas.Background = new SolidColorBrush(Color.FromRgb(0, 0, 255));
            newCanvas.Margin = new Thickness(0);

            ((FlatDisplay)obj).MainCanvas.Children.Add(newCanvas);



            VisualBrush b;
            if (((FlatDisplay)obj).LastBrush == null)
                b = new VisualBrush(rectangle1);
            else
                b = new VisualBrush(((FlatDisplay)obj).LastBrush);

            TransitionEffect ef;

            ef = ((FlatDisplay)obj).GetTransitionEffect();
            Random r = new Random();
            //switch (r.Next(3))
            //{
            //    case 0:
            //        ef = new TransitionEffects.BlindsTransitionEffect();
            //        break;
            //    case 1:
            //        ef = new TransitionEffects.BloodTransitionEffect();
            //        break;
            //    case 2:
            //        ef = new TransitionEffects.RotateCrumbleTransitionEffect();
            //        break;
            //    case 3:
            //        ef = new TransitionEffects.CircleStretchTransitionEffect();
            //        break;
            //    default:
            //        ef = new TransitionEffects.CrumbleTransitionEffect();
            //        break;

            //}
            newCanvas.Effect = ef;
            ((TransitionEffect)newCanvas.Effect).OldImage = b;
            Storyboard sb = new Storyboard();

            Timeline tl = new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 1)));
            sb.Children.Add(tl);

            string stGuid = "ef" + Guid.NewGuid().ToString().Replace("-", "");
            ((FlatDisplay)obj).RegisterName(stGuid, ef);
            Storyboard.SetTargetName(sb, stGuid);
            Storyboard.SetTargetProperty(sb, new PropertyPath(TransitionEffect.ProgressProperty));
            sb.Completed += new EventHandler(((FlatDisplay)obj).sb_Completed);
            sb.Begin((FlatDisplay)obj);
            ((FlatDisplay)obj).LastBrush = newCanvas;
            ((FlatDisplay)obj).MainCanvas.Children.RemoveAt(0);

        }

        public string LyricControl
        {
            get
            {

                object result = GetValue(LyricControlProperty);
                return (string)result;
            }
            set
            {

                SetValue(LyricControlProperty, value);
            }
        }

        #endregion

    }
}
